package com.chinaoly.baotou.framework.elasticsearch;

import com.chinaoly.baotou.framework.huawei.LoginUtil;
import com.chinaoly.baotou.framework.huawei.KerberosProperties;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class RestClientConfig {

    @Value("${elasticsearch.hostname:}")
    private String hostname ;

    @Value("${elasticsearch.port:}")
    private int port ;

    @Value("${elasticsearch.hwhosts:null}")
    private String hwhosts;
    @Resource
    private KerberosProperties kerberosProperties;

    public  static final RequestOptions COMMON_OPTIONS;
    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
        COMMON_OPTIONS = builder.build();
    }

    /**
     * HighLevelRestConfig
     *
     * @param
     * @return org.elasticsearch.client.RestClient
     * @author wliduo[i@dolyw.com]
     * @date 2019/8/12 18:56
     */
    @Bean
    public RestHighLevelClient restHighLevelClient() {
        // 如果有多个从节点可以持续在内部new多个HttpHost，参数1是IP，参数2是端口，参数3是通信协议
        return new RestHighLevelClient(getRestClientBuilder(getHostArray(hwhosts)));
    }

    private HttpHost[] getHostArray(String esServerHost) {
        String schema = "https";
        if (!kerberosProperties.isEsEnable()) {
            //非华为环境
            esServerHost = hostname+":"+port;
            schema = "http";
        }
        List<HttpHost> hosts = new ArrayList<HttpHost>();
        String[] hostArray1 = esServerHost.split(",");

        for (String host : hostArray1) {
            String[] ipPort = host.split(":");
            HttpHost hostNew = new HttpHost(ipPort[0], Integer.valueOf(ipPort[1]), schema);
            hosts.add(hostNew);
        }
        return hosts.toArray(new HttpHost[] {});
    }

    /**
     * 获取es客户端
     * @param HostArray
     * @return
     * @throws Exception
     */
    public RestClientBuilder getRestClientBuilder(HttpHost[] HostArray) {

        RestClientBuilder builder = null;
        if (kerberosProperties.isEsEnable()) {
            //华为es需要鉴权
            setSecConfig();
        } else {
            System.setProperty("es.security.indication", "false");
        }
        builder = RestClient.builder(HostArray);
        Header[] defaultHeaders = new Header[] {new BasicHeader("Accept", "application/json"),
                new BasicHeader("Content-type", "application/json")};

        builder.setDefaultHeaders(defaultHeaders);

        return builder;
    }

    private void setSecConfig() {
        try {
            LoginUtil.setJaasFile(kerberosProperties.getPrincipal(), kerberosProperties.getKeytab());
            LoginUtil.setKrb5Config(kerberosProperties.getConf());
            System.setProperty("elasticsearch.kerberos.jaas.appname", "EsClient");
            System.setProperty("es.security.indication", "true");
        } catch (Exception e) {
        }
    }

}
